package com.monitor.module.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.monitor.entity.enums.RoleEnum;
import com.monitor.entity.pojo.company.Company;
import com.monitor.entity.pojo.user.User;
import com.monitor.entity.request.user.UserCompanyRequest;
import com.monitor.entity.response.ResultCode;
import com.monitor.entity.response.Rs;
import com.monitor.module.company.mapper.CompanyMapper;
import com.monitor.module.company.service.CompanyService;
import com.monitor.module.user.mapper.UserMapper;
import com.monitor.module.user.service.UserService;
import com.monitor.utils.PoJoUtil;
//import com.monitor.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * .
 *
 * @author Li Zhiyuan
 * @since 2020-11-10
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper mapper;
    @Resource
    private CompanyService companyService;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private RedisTemplate<String,User> redisTemplate;

    @Override
    public ResponseEntity<Rs<Object>> register(User pojo) {
        //获取当前管理员的企业id并给注册员工
//        pojo.setCompanyId(UserUtil.getSession().getCompanyId());
        if (getOne(Wrappers.<User>lambdaQuery()
                .eq(User::getUserName, pojo.getUserName())
                .eq(User::getCompanyId, pojo.getCompanyId())) == null) {
            //注册员工账号
            pojo.setRoleId(RoleEnum.STAFF.getValue());
            save(pojo);
            log.info("注册员工账号执行结果:[{}]", ResultCode.SUCCESS.getMsg());
            return Rs.ok(ResultCode.SUCCESS);
        }
        log.info("注册员工账号执行结果:[{}]", ResultCode.COMPANY_TEL_EXIST.getMsg());
        return Rs.fail(ResultCode.COMPANY_TEL_EXIST);
    }

    @Override

    public ResponseEntity<Rs<Object>> superregister(UserCompanyRequest request) {
        //企业名称
        String companyName = request.getCompanyRequest().getName();
        if (companyService.getOne(Wrappers.<Company>lambdaQuery().eq(Company::getName, companyName)) != null)
            return Rs.fail(ResultCode.ENTERPRISE_NAME);
        Company company = PoJoUtil.transToPoJo(request.getCompanyRequest(), Company.class);
        //创建企业
        companyMapper.insert(company);

        //根据企业id创建管理员
        User user = PoJoUtil.transToPoJo(request, User.class);
        user.setCompanyId(company.getId());
        if (getOne(Wrappers.<User>lambdaQuery()
                .eq(User::getUserName, user.getUserName())
                .eq(User::getCompanyId, user.getCompanyId())) == null) {
            //注册公司账号
            user.setRoleId(RoleEnum.COMPANY.getValue());
            save(user);
            return Rs.ok(ResultCode.SUCCESS);
        }
        return Rs.fail(ResultCode.COMPANY_TEL_EXIST);
    }
}
